<template>
  <div class="app-container">
    <div class="flex-between">
      <div class="flex-between">
        <el-button
          type="primary"
          @click="handleInAddPrescription"
        >新增药方</el-button>
      </div>
      <el-input
        v-model="search"
        class="search-main"
        placeholder="请输入用户的名字查询"
        prefix-icon="el-icon-search"
        @keyup.enter.native="HandleSearch"
      />
    </div>
    <el-table
      :data="prescriptionList"
      style="width: 100%;margin-top:30px;"
      border
    >
      <el-table-column
        align="center"
        label="处方编号"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="医师姓名"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.adminName }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="病人姓名"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="病因"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.pathogeny }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        align="center"
        label="所用药物"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column> -->
      <el-table-column
        align="center"
        label="是否已抓药"
        width=""
      >
        <template slot-scope="scope">
          <span :style="scope.row.payment === 0?'color:red':'color:green'"> {{ scope.row.payment === 0?'待拿药':'已完成' }} </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="药方总价格"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.amount }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="用药开始时间"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.medicationTimeStart }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="用药结束时间"
      >
        <template slot-scope="scope">
          {{ scope.row.medicationTimeEnd }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        width="250px"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleDetail(scope)"
          >查看详细</el-button>
          <el-button
            type="danger"
            :disabled="scope.row.payment === 1"
            size="small"
            @click="handleDelete(scope)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page="pageNum"
      :page-size="pageSize"
      layout="total,sizes,  prev, pager, next, jumper"
      :total="totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      :visible.sync="dialogVisible"
      title="药方详情"
    >
      <Activitysee
        :prescription="prescription"
        @cancel="cancel"
      />
    </el-dialog>

  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getPrescription, getPrescriptionInfoById, updatePresciption, deletePrescriptionMedicine, deletePresciption } from '@/api/prescription'
import Activitysee from './components/Activitysee'
import { timestampToTimeDay } from '@/utils/time'

const defaultPrescription = {
  pathogeny: '',
  day: '',
  type: '1',
  remake: '',
  adminId: '',
  userId: '',
  userInfoVO: {
    username: '',
    age: ''
  }
}

export default {
  components: {
    Activitysee
  },
  data() {
    return {
      routes: [],
      prescriptionList: [],
      dialogVisible: false,
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      pageNum: 1, // 默认每页显示1条数据
      pageSize: 10,
      loading: false,
      totalCount: 0,
      search: '',
      prescription: Object.assign({}, defaultPrescription)
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    // this.getRoutes()
    if (this.$store.state.user.roles === 'doctor') {
      this.getPrescription({
        adminId: this.$store.state.user.adminId,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        type: 1
      })
    } else {
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        type: 1
      })
    }
  },
  methods: {
    async getPrescription(data) {
      const res = await getPrescription(data)
      const list = res.data.list
      for (const i in list) {
        list[i].medicationTimeStart = timestampToTimeDay(list[i].medicationTimeStart)
        list[i].medicationTimeEnd = timestampToTimeDay(list[i].medicationTimeEnd)
      }
      this.prescriptionList = list
      this.totalCount = res.data.total
    },
    async handleDetail(scope) { // 查看详情
      this.dialogVisible = true
      const res = await getPrescriptionInfoById({ id: scope.row.id })
      console.log(res)
      res.data.medicationTimeStart = timestampToTimeDay(res.data.medicationTimeStart)
      res.data.medicationTimeEnd = timestampToTimeDay(res.data.medicationTimeEnd)
      this.prescription = res.data
    },
    generateArr(routes) {
      let data = []
      routes.forEach(route => {
        data.push(route)
        if (route.children) {
          const temp = this.generateArr(route.children)
          if (temp.length > 0) {
            data = [...data, ...temp]
          }
        }
      })
      return data
    },
    handleDelete({ $index, row }) {
      this.$confirm('确定要删除吗?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(row)
        row.disable = 1
        const data = {}
        data.id = row.id
        await deletePresciption(data)
        this.prescriptionList.splice($index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
        .catch(err => { console.error(err) })
    },
    handleSizeChange(page) {
      this.pageSize = page
      this.getPrescription({
        adminId: this.$store.state.user.roles === 'doctor' ? this.$store.state.user.adminId : '',
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    handleCurrentChange(page) {
      this.pageNum = page
      this.getPrescription({
        adminId: this.$store.state.user.roles === 'doctor' ? this.$store.state.user.adminId : '',
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    cancel() {
      this.dialogVisible = false
    },
    HandleSearch() {
      this.getPrescription({
        adminId: this.$store.state.user.roles === 'doctor' ? this.$store.state.user.adminId : '',
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        username: this.search
      })
    },
    handleInAddPrescription() {
      this.$router.push('/doctor/prescriptionDetail/:prescriptionId')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
